home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Navigation Services SDK / Examples / Sampler / Sampler ƒ / Utilities.c < prev   
Encoding:
Text File  |  1999-06-16  |  4.4 KB  |  186 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Utilities.c
  3.  
  4.     Copyright:    © 1997-1998 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. //    
  9. //    You may incorporate this sample code into your applications
  10. //    without restriction. This sample code has been provided "AS
  11. //    IS" and the responsibility for its operation is 100% yours.
  12. //    You are not permitted to redistribute the source as "Apple
  13. //    sample code" after having made changes. If you're going to
  14. //    re-distribute the source, we require that you make it clear
  15. //    in the source that the code was descended from Apple sample
  16. //    code, but that you've made changes.
  17. //    
  18.  
  19. #pragma segment AppSeg
  20.  
  21. #ifndef __SCRAP__
  22. #include <Scrap.h>
  23. #endif
  24.  
  25. #ifndef __TEXTEDIT__
  26. #include <TextEdit.h>
  27. #endif
  28.  
  29. #ifndef __MEMORY__
  30. #include <Memory.h>
  31. #endif
  32.  
  33. #ifndef __TEXTUTILS__
  34. #include <TextUtils.h>
  35. #endif
  36.  
  37.  
  38.  
  39. short myTECut(TEHandle theTE);
  40. short myTEPaste(TEHandle theTE, short* spaceBefore, short* spaceAfter);
  41. short TEIsFrontOfLine(short offset, TEHandle theTE);
  42. short TEGetLine(short offset, TEHandle theTE);
  43.  
  44.  
  45. // *****************************************************************************
  46. // *
  47. // *    myTECut()
  48. // *
  49. // *****************************************************************************
  50. short myTECut(TEHandle theTE)
  51. {    
  52.     OffsetTable    startOffsets;
  53.     OffsetTable    endOffsets;
  54.     short        selStart, selEnd, characters;
  55.     Handle        theScrap;
  56.  
  57.     if (!(characters = (**theTE).selStart - (**theTE).selEnd))
  58.         return 0;
  59.  
  60.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selStart,true,0L,startOffsets);
  61.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selEnd,false,0L,endOffsets);
  62.  
  63.     if ((startOffsets[0].offFirst == (**theTE).selStart) &&
  64.         (endOffsets[0].offSecond  == (**theTE).selEnd))
  65.         {
  66.         // both the beginning and end of the current selection is on word boundaries
  67.         selStart = (**theTE).selStart;
  68.         selEnd = (**theTE).selEnd;
  69.         if ((*((**theTE).hText))[selStart - 1] == ' ')
  70.             {
  71.             TESetSelect(selStart - 1,selEnd,theTE);
  72.             TECut(theTE);
  73.             theScrap = TEScrapHandle();
  74.             BlockMove((char*)(*theScrap) + 1,(*theScrap),TEGetScrapLength() - 1);
  75.             TESetScrapLength(TEGetScrapLength() - 1);
  76.             ZeroScrap();
  77.             TEToScrap();
  78.             characters--;
  79.             }
  80.         else
  81.             if ((*((**theTE).hText))[selEnd] == ' ')
  82.                 {
  83.                 TESetSelect(selStart,selEnd + 1,theTE);
  84.                 TECut(theTE);
  85.                 TESetScrapLength(TEGetScrapLength() - 1);
  86.                 ZeroScrap();
  87.                 TEToScrap();
  88.                 characters--;
  89.                 }
  90.             else
  91.                 TECut(theTE);
  92.         }
  93.     else
  94.         TECut(theTE);
  95.  
  96.     return characters;
  97. }
  98.  
  99.  
  100. // *****************************************************************************
  101. // *
  102. // *    myTEPaste()
  103. // *
  104. // *****************************************************************************
  105. short myTEPaste(TEHandle theTE, short* spaceBefore, short* spaceAfter)
  106. {    
  107.     OffsetTable    startOffsets;
  108.     OffsetTable    endOffsets;
  109.     short        addSpaceAfter;
  110.     short        characters;
  111.  
  112.     characters = (**theTE).selStart - (**theTE).selEnd;
  113.  
  114.     if (spaceBefore)
  115.         *spaceBefore = false;
  116.     if (spaceAfter)
  117.         *spaceAfter = false;
  118.  
  119.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selStart,false,0L,startOffsets);
  120.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selEnd,true,0L,endOffsets);
  121.  
  122.     addSpaceAfter = ((endOffsets[0].offFirst == (**theTE).selEnd) && ((*((**theTE).hText))[(**theTE).selEnd] != ' '));
  123.  
  124.     if ((startOffsets[0].offSecond == (**theTE).selStart) && ((*((**theTE).hText))[(**theTE).selStart - 1] != ' '))
  125.         {
  126.         TEKey(' ',theTE);
  127.         characters++;
  128.         if (spaceBefore)
  129.             *spaceBefore = true;
  130.         }
  131.  
  132.     TEPaste(theTE);
  133.     characters += TEGetScrapLength();
  134.  
  135.     if (addSpaceAfter)
  136.         {
  137.         TEKey(' ',theTE);
  138.         characters++;
  139.         if (spaceAfter)
  140.             *spaceAfter = true;
  141.         }
  142.         
  143.     return characters;
  144. }
  145.  
  146.  
  147. // *****************************************************************************
  148. // *
  149. // *    TEIsFrontOfLine()
  150. // *
  151. // *****************************************************************************
  152. short TEIsFrontOfLine(short offset, TEHandle theTE)
  153. {    
  154.     short line = 0;
  155.  
  156.     if ((**theTE).teLength == 0)
  157.         return(true);
  158.  
  159.     if (offset >= (**theTE).teLength)
  160.         return ((*((**theTE).hText))[(**theTE).teLength - 1] == 0x0d);
  161.  
  162.     while ((**theTE).lineStarts[line] < offset)
  163.         line++;
  164.  
  165.     return ((**theTE).lineStarts[line] == offset);
  166. }
  167.  
  168.  
  169. // *****************************************************************************
  170. // *
  171. // *    TEGetLine()
  172. // *
  173. // *****************************************************************************
  174. short TEGetLine(short offset, TEHandle theTE)
  175. {
  176.     short line = 0;
  177.  
  178.     if (offset > (**theTE).teLength)
  179.         return((**theTE).nLines);
  180.  
  181.     while ((**theTE).lineStarts[line] < offset)
  182.         line++;
  183.     
  184.     return line;
  185. }
  186.